#include "flight.h"
#include <vector>

using namespace std;
using namespace aviation;

//Constructors

Flight::Flight()
	:
    m_flightNumber("AA001"),
	m_date("01012012"),
	m_time("0915"),
	m_source("AAA"),
	m_dest("AAA")
{
	m_maxStandardSeats = 200;
	m_maxFirstSeats = 20;
	m_ID = util::HashString(AsString());
}


Flight::Flight(const String& fcgString) //fcgString is a line from FCGDATA file
{
	vector<string> flightDetails = util::TokeniseString(fcgString);

	//Check size of vector is 7
	if (flightDetails.size() < 7) throw exception("Not enough values in FCGDATA input");
	if (flightDetails.size() > 7) throw exception("Too many values in FCGDATA input");

	m_flightNumber = FlightNumber(flightDetails[0]);
	m_date = Date(flightDetails[1]);
	m_time = Time(flightDetails[2]);
	m_source = Airport(flightDetails[3]);
	m_dest = Airport(flightDetails[4]);	
	m_maxStandardSeats = std::stoi(flightDetails[5]);
	m_maxFirstSeats = std::stoi(flightDetails[6]);

	//Check for a valid number of seats.
	if (!(m_maxStandardSeats >= 0 && m_maxStandardSeats <= 1500)) throw exception("Attempted to enter an invalid number of seats.");
	if (!(m_maxFirstSeats >= 0 && m_maxFirstSeats <= 1500)) throw exception("Attempted to enter an invalid number of seats.");

	m_ID = util::HashString(AsString());
}

Flight::Flight(const Flight& other)
{
	*this = other;
}

Flight::~Flight()
{
}


Flight::Flight(const FlightNumber& flightNumber, const Date& date, const Time& time, const Airport& source, const Airport& destination, const unsigned int standardSeats, const unsigned int firstSeats)
	:
    m_flightNumber(flightNumber),
	m_date(date),
	m_time(time),
	m_source(source),
	m_dest(destination),
	m_maxStandardSeats(standardSeats),
	m_maxFirstSeats(firstSeats)
{
	m_ID = util::HashString(AsString());
}

//Operators

Flight& Flight::operator=(const Flight& other)
{
	m_flightNumber = other.m_flightNumber;
	m_date = other.m_date;
	m_time = other.m_time;
	m_source = other.m_source;
	m_dest = other.m_dest;
	m_maxStandardSeats = other.m_maxStandardSeats;
	m_maxFirstSeats = other.m_maxFirstSeats;
	m_ID = other.m_ID;

	return *this;
}

const bool Flight::operator==(const Flight& other) const
{
	return m_ID == other.m_ID;
}

const bool Flight::operator!=(const Flight& other) const
{
	return !(*this == other);
}

const bool Flight::operator<(const Flight& other) const
{
	return m_ID < other.m_ID;
}

//Accessors

const FlightNumber Flight::GetFlightNumber() const
{
	return m_flightNumber;
}

const Date Flight::GetDate() const
{
	return m_date;
}

const Time Flight::GetTime() const
{
	return m_time;
}

const Airport Flight::GetSource() const
{
	return m_source;
}

const Airport Flight::GetDestination() const
{
	return m_dest;
}

const unsigned Flight::GetMaxStandardSeats() const
{
	return m_maxStandardSeats;
}

const unsigned Flight::GetMaxFirstSeats() const
{
	return m_maxFirstSeats;
}

const unsigned Flight::GetID() const
{
	return m_ID;
}

const Flight::String Flight::AsString() const
{
	String s = " ";
	return m_flightNumber.AsString() 
		+ s + m_date.AsString()
		+ s + m_time.AsString() 
		+ s + m_source.AsString() 
		+ s + m_dest.AsString()
		+ s + util::XtoA(m_maxStandardSeats)
		+ s + util::XtoA(m_maxFirstSeats);
}